package com.fifth.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Cart;
import com.fifth.order.mapper.CartMapper;
import com.fifth.order.service.CartService;
import com.fifth.order.vo.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @作者：刘壬杉
 * @创建时间 2022/9/1 14:20
 **/
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 点击加入购物车按钮添加商品到购物车
     * @param cart
     * @return
     */
    @Override
    public CommonResult addCart(Cart cart) {
        //获取userid
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap!=null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
        }else {
            return new CommonResult(4000,"登录失效,请登录后再购买",null);
        }
        cart.setUid(userid);
        //判断相同订单在购物车中是否已经存在
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",cart.getUid());
        queryWrapper.eq("goods_id",cart.getGoodsId());
        queryWrapper.eq("is_deleted",0);
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        if (carts.size()!=0){
            return new CommonResult(6000,"该商品已存在,请勿重复添加",null);
        }else {
            //添加商品到购物车
            int insert = cartMapper.insert(cart);
            if (insert != 1) {
                return new CommonResult(5000, "添加购物车失败", null);
            }
            return new CommonResult(2000, "添加成功", null);
        }
    }

    /**
     * 点击我的购物车查看购物车信息
     * @return
     */
    @Override
    public CommonResult findCart(Integer currentPage, Integer pageSize) {
        //获取userid
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid和手机号
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap!=null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
        }else {
            return new CommonResult(4000,"登录失效,请登录后再购买",null);
        }
        IPage<Cart> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",userid);
        IPage<Cart> cartInfo = cartMapper.findCart(page,queryWrapper);
        List<Cart> records = cartInfo.getRecords();
        for (Cart record : records) {
            BigDecimal price = record.getPrice();
            Integer num = record.getNum();
            record.setPrice(price.divide(new BigDecimal(num)));
        }
        return new CommonResult(2000,"查询成功",cartInfo);
    }

    /**
     * 根据购物车id删除购物车
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteCart(String id) {
        int i = cartMapper.deleteById(id);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }

    /**
     * 根据ids批量删除
     * @param ids
     * @return
     */
    @Override
    public CommonResult deleteByIds(String[] ids) {
        List<String> list = Arrays.asList(ids);
        cartMapper.deleteBatchIds(list);
        return new CommonResult(2000,"删除成功",null);
    }


}
